using Avalonia.Controls;
using Avalonia.Interactivity;
using IntegratedMeasuring.ViewModels.Entity;
using System;
using System.Threading.Tasks;
using EntityGC = IntegratedMeasuring.ViewModels.Entity.GC;

namespace IntegratedMeasuring.Pages;

public partial class DuplicateCodeDialog : Window
{
    public EntityGC.DuplicateCodeHandlingType SelectedOption { get; private set; }
    private bool TriggeredByButton = false;
    
    public DuplicateCodeDialog(string message)
    {
        InitializeComponent();
        MessageTextBlock.Text = message;
        
        // 禁用窗口关闭按钮和Esc键
        this.Closing += OnClosing;
    }
    
    private void OnClosing(object? sender, WindowClosingEventArgs e)
    {
        // 阻止用户通过X按钮或Esc键关闭窗口
        e.Cancel = !TriggeredByButton;
    }
    
    private void SaveAsNewButton_Click(object? sender, RoutedEventArgs e)
    {
        SelectedOption = EntityGC.DuplicateCodeHandlingType.SaveAsNew;
        TriggeredByButton = true;
        this.Close();
    }
    
    private void OverwriteButton_Click(object? sender, RoutedEventArgs e)
    {
        SelectedOption = EntityGC.DuplicateCodeHandlingType.OverwriteOld;
        TriggeredByButton = true;
        this.Close();
    }
    
    private void RescanButton_Click(object? sender, RoutedEventArgs e)
    {
        SelectedOption = EntityGC.DuplicateCodeHandlingType.AskUser; // 表示重新扫码
        TriggeredByButton = true;
        this.Close();
    }
    
    /// <summary>
    /// 显示对话框并返回用户选择
    /// </summary>
    /// <param name="owner">父窗口</param>
    /// <param name="message">提示消息</param>
    /// <returns>用户选择的处理方式</returns>
    public static async Task<EntityGC.DuplicateCodeHandlingType> ShowDialogAsync(Window owner, string message)
    {
        var dialog = new DuplicateCodeDialog(message);
        await dialog.ShowDialog(owner);
        return dialog.SelectedOption;
    }
} 